<?php

namespace App\Http\Controllers\Api\V2;

use App\Http\Controllers\Controller;
use App\Lib\PhonePePay\PhonePePaymentApi;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PaytmController extends Controller
{

    public function payPage(Request $request)
    {
        $merchantTransactionId = $request->merchant_transaction_id;
        $mobileNumber = $request->mobile_number;
        $merchantUserId = $request->merchant_user_id;
        $amountInPaise = $request->amount;
        $callbackUrl = $request->callback_url;
        $redirectUrl = $request->redirect_url;
        $PhonePePayment = new PhonePePaymentApi();
        $res = $PhonePePayment->getPayPage($merchantTransactionId,$mobileNumber,$merchantUserId,$amountInPaise,$callbackUrl,$redirectUrl);
        return $this->success($res);
    }

    public function checkStatus(Request $request)
    {
        $merchantTransactionId = $request->merchant_transaction_id;
        $PhonePePayment = new PhonePePaymentApi();
        $res = $PhonePePayment->checkStatus($merchantTransactionId);
        return $this->success($res);
    }

    public function refund(Request $request)
    {
        $originalTransactionId = $request->original_transaction_id;
        $merchantTransactionId = $request->merchant_transaction_id;
        $amountInPaise = $request->amount;
        $PhonePePayment = new PhonePePaymentApi();
        $res = $PhonePePayment->refund($originalTransactionId,$merchantTransactionId,$amountInPaise);
        return $this->success($res);
    }

     public function verifyCallback(Request $request)
     {
         $xVerify = $request->x_verify;
         $response = $request->response;
         $PhonePePayment = new PhonePePaymentApi();
         $res = $PhonePePayment->verifyCallback($xVerify,$response);

         Log::error('====verifyCallback====', ['xVerify' => $xVerify,'response'=>$response,'res' => $res]);
         if(!empty($res)){
             return $this->success($res);
         }else{
             return $this->failure("Payment failed");
         }

     }

     public function orderConfirmed(Request $request)
     {
         $combined_order_id = $request['combined_order_id'] ?? 0;
         if(!empty($combined_order_id)){
            header('Location: https://www.dripculture.in/checkout/order-confirmed?combined_order_id='.$combined_order_id);
         }else{
            header('Location: https://www.dripculture.in');
         }
     }

}
